var addBinary = function (a, b) {
  const max = Math.max(a.length, b.length);
  a = a.padStart(max, "0");
  b = b.padStart(max, "0");
  let ans = "";
  let di = 0;
  for (let n = max; n > 0; n--) {
    const left = a.charAt(n - 1);
    const right = b.charAt(n - 1);
    const sum = Number(left) + Number(right) + di;
    di = sum > 1 ? 1 : 0;
    ans = sum - di * 2 + ans;
  }
  if (di > 0) {
    return "1" + ans;
  }
  return ans;
};

const ans = addBinary("1010", "1011");
console.log("ans: ", ans);
